package studentinformationmanagementsystem.service.impl;

import studentinformationmanagementsystem.dao.DepartmentDao;
import studentinformationmanagementsystem.dao.impl.DepartmentDaolmpl;
import studentinformationmanagementsystem.entity.Classandgrade;
import studentinformationmanagementsystem.entity.Department;
import studentinformationmanagementsystem.entity.Page;
import studentinformationmanagementsystem.service.DepartmentService;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class DepartmentServicelmpl implements DepartmentService {


    DepartmentDao departmentDao=new DepartmentDaolmpl();

    /**
     * 添加院系信息
     *
     * @param department
     */
    @Override
    public int register(Department department) {
        department.setIsDelete(0);
        return departmentDao.insert(department);
    }

    /**
     *  根据ID更新院系信息
     * @param department  院系对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Department department) {
        return departmentDao.update(department);
    }

    /**
     *  根据ID删除院系
     * @param department 院系ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Department department) {
        return departmentDao.delete(department);
    }

    /**
     * 查询所有院系信息
     * @return 所有院系信息
     */
    @Override
    public List<Department> queryAll() {
        Department department = new Department();
        department.setIsDelete(0);
        return departmentDao.queryAll(department);
    }

    /**
     * 根据院系ID查询院系信息
     * @param department 院系ID
     * @return 院系信息
     */
    @Override
    public Department queryById(Department department) {
        return departmentDao.queryById(department);
    }

    /**
     * 分页查询
     * @param pageNo  查询页码
     * @param pageSize  每页的记录数
     * @return
     */
    @Override
    public Page<Department> page(int pageNo, int pageSize) {
        /**
         * 当前页码:pageNo
         * 总页码:pageTotal
         * 当前页显示的数量:pageSize
         * 总记录数:pageTotalCount
         * 当前页数据:items
         */

        //访问页面的记录条数的开始行数：当前页码-1*每页展示的记录行数
        int bigin=(pageNo-1)*pageSize;


        //总记录数量
        Integer pageTotalCount=departmentDao.queryForPageTotalCount();

        //总页码数量=总记录数/每页记录显示数量，然后判断是否可以被除尽，如果不能除尽，那么总的页码数量就需要+1
        Integer pageTotal=pageTotalCount/pageSize;

        if (pageTotalCount%pageSize!=0){
            pageTotal++;
        }


        //当前页码数据
        List<Department> items = departmentDao.queryForPageItems(bigin, pageSize);

        Page<Department> objectPage = new Page<Department>(pageNo,pageTotal,pageSize,pageTotalCount,items);

        return objectPage;
    }

    /**
     * 检查院系名称是否已存在
     * @param department
     * @return true 院系名称已存在
     */
    @Override
    public boolean isExistsDepartmentNname(Department department) {

        boolean falg=false;

        Department department1 = departmentDao.queryBydepartmentNname(department);

        if (department1!=null){
            falg=true;
        }

        return falg;

    }

    @Override
    public Integer queryDepartmentCount() {
        return departmentDao.queryForPageTotalCount();
    }
}
